package com.rxyl.love.petdoctor.util;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.rxyl.love.petdoctor.app.MyApplication;
import com.rxyl.love.petdoctor.constant.Urls;

import java.util.Map;
import java.util.Set;

/**
 * Created by Administrator on 2016/1/11 0011.
 */
public class NetworkUtil {

    private static NetworkUtil networkUtil = new NetworkUtil();
    private HttpUtils httpUtils;

    private NetworkUtil() {
        httpUtils = MyApplication.getApp().getHttpUtils();
    }

    public static NetworkUtil getInstance() {
        return networkUtil;
    }

    /**
     * 获取首页广告信息
     */
    public void getBannaer(RequestCallBack callback) {
        doGet(Urls.BANNER_LIST, null, callback);
    }

    /**
     * 获取圈子信息
     */
    public void getCircle(RequestCallBack callBack) {
        doGet(Urls.CIRCLE, null, callBack);
    }
    /**
     * 获取话题信息
     */
    public void getTopic(RequestCallBack callBack){doGet(Urls.TOPICS,null,callBack);}

    /**
     * 获取首页关键字
     */
    public void getKeyWords(RequestCallBack callback) {
        doGet(Urls.KEY_WORDS, null, callback);
    }

    /**
     * 获取好医生
     */
    public void getGoodDoctor(String url,RequestCallBack callback) {
        doGet(url, null, callback);
    }

    /**
     * 获取周边医院
     */
    public void getNearbyHospital(String url,RequestCallBack callback) {
        doGet(url, null, callback);
    }


    /**
     * 获取城市列表
     */
    public void getCityList(RequestCallBack callback) {
        doGet(Urls.CITY_LIST, null, callback);
    }

    /**
     * 获取医生信息
     */
    public void getFindDoctor(String url,RequestCallBack callback) {
        doGet(url, null, callback);
    }

    /**
     * 获取医院列表
     */
    public void getHospital(String url, RequestCallBack callback) {
        doGet(url, null, callback);
    }

    /**
     * 获取评价列表
     */
    public void getComment(String url, RequestCallBack callback) {
        doGet(url, null, callback);
    }

    /**
     * 获取搜索信息
     */
    public void getSearchResult(String url, RequestCallBack callback) {
        doGet(url, null, callback);
    }

    /**
     * /**
     * get请求
     *
     * @param requestUrl
     * @param params
     */
    private void doGet(String requestUrl, Map<String, Object> params, final RequestCallBack callback) {
        RequestParams requestParams = new RequestParams();

        if (params != null) {
            Set<Map.Entry<String, Object>> set = params.entrySet();

            for (Map.Entry<String, Object> entry : set) {
                if (entry.getValue() != null && !"".equals(entry.getValue())) {
                    requestParams.addQueryStringParameter(
                            entry.getKey(), String.valueOf(entry.getValue()));
                }
            }
        }

        httpUtils.send(HttpRequest.HttpMethod.GET,
                requestUrl,
                requestParams,
                new RequestCallBack<String>() {

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        callback.onSuccess(responseInfo);
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        callback.onFailure(error, msg);
                    }
                });
    }


    /**
     * post请求
     *
     * @param requestUrl
     * @param params
     */
    private void doPost(
            String requestUrl, Map<String, Object> params, final RequestCallBack callback) {
        RequestParams requestParams = new RequestParams();

        if (params != null) {
            Set<Map.Entry<String, Object>> set = params.entrySet();

            for (Map.Entry<String, Object> entry : set) {
                if (entry.getValue() != null && !"".equals(entry.getValue())) {
                    requestParams.addBodyParameter(
                            entry.getKey(),
                            String.valueOf(entry.getValue()));
                }
            }
        }

        httpUtils.send(HttpRequest.HttpMethod.POST,
                requestUrl,
                requestParams,
                new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        callback.onSuccess(responseInfo);
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        callback.onFailure(error, msg);
                    }
                });
    }

}
